package com.oss.test;
import java.io.File; 
import java.io.IOException; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.SAXException; 

public class DOMParser { 
 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); 

  public Document parse(String filePath) { 
     Document document = null; 
     try { 
        //DOM parser instance 
        DocumentBuilder builder = builderFactory.newDocumentBuilder(); 
        //parse an XML file into a DOM tree 
        document = builder.parse(new File(filePath)); 
     } catch (ParserConfigurationException e) { 
        e.printStackTrace();  
     } catch (SAXException e) { 
        e.printStackTrace(); 
     } catch (IOException e) { 
        e.printStackTrace(); 
     } 
     return document; 
  } 
    
  public static void main(String[] args) { 
        DOMParser parser = new DOMParser(); 
        Document document = parser.parse("d:\\province_data.xml"); 
        //get root element 
        Element rootElement = document.getDocumentElement(); 
 
        //traverse child elements 
        //省
        NodeList nodes = rootElement.getChildNodes(); 
        for (int i=0; i < nodes.getLength(); i++) 
        { 
           Node node = nodes.item(i); 
           if (node.getNodeType() == Node.ELEMENT_NODE) {   
              Element child = (Element) node; 
              String zipcode1 = child.getAttribute("zipcode");
              System.out.println("('"+child.getAttribute("name")+"',"+zipcode1+",0),");
              //市
              NodeList nodes2 = child.getChildNodes(); 
              for (int j=0; j < nodes2.getLength(); j++) 
              { 
                 Node node2 = nodes2.item(j); 
                 if (node2.getNodeType() == Node.ELEMENT_NODE) {   
                    Element child2 = (Element) node2; 
                    String zipcode2 = child2.getAttribute("zipcode");
                    System.out.println("('"+child2.getAttribute("name")+"',"+zipcode2+","+zipcode1+"),");
                    //县
                    NodeList nodes3 = child2.getChildNodes(); 
                    for (int k=0; k < nodes3.getLength(); k++) 
                    { 
                       Node node3 = nodes3.item(k); 
                       if (node3.getNodeType() == Node.ELEMENT_NODE) {   
                          Element child3 = (Element) node3; 
                          String zipcode3 = child3.getAttribute("zipcode");
                          System.out.println("('"+child3.getAttribute("name")+"',"+zipcode3+","+zipcode2+"),");
                          
                       } 
                    } 
                 } 
              } 
           } 
        } 
        
        NodeList nodeList = rootElement.getElementsByTagName("book"); 
        if(nodeList != null) 
        { 
           for (int i = 0 ; i < nodeList.getLength(); i++) 
           { 
              Element element = (Element)nodeList.item(i); 
              String id = element.getAttribute("id"); 
           } 
        } 
  } 
}